package engine.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import engine.dataobjects.MailItem;
import engine.dataobjects.MailItemInstance;
import engine.dataobjects.xml.Active;
import engine.dataobjects.xml.Inactive;
import engine.dataobjects.xml.Items;
import engine.services.IDataResource;

public class ConfigResourceJaxbImpl implements IDataResource<engine.dataobjects.xml.MailItem, Object>, ResourceLoaderAware {
	private Log log = LogFactory.getLog(ConfigResourceJaxbImpl.class);
	
	private Items items;
	
	String configResourceFilename;
	
	ResourceLoader resourceLoader;

	private ConfigResourceJaxbImpl() {
		
	}
	
	@Override
	public void init() {
		load();
	}

	@Override
	public void load() {
		try {
			Resource configResource = (Resource)resourceLoader.getResource(this.configResourceFilename);
			log.info("****** Loading configuration from name: " + configResource.getFilename());
			log.info("****** Loading configuration from URL: " + configResource.getURL());		

			
			JAXBContext context = JAXBContext.newInstance(Items.class);
			this.items = (Items) context.createUnmarshaller().unmarshal(configResource.getFile());
		} catch (Exception e) {
			log.error("context loading", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void refresh() {
		load();

	}

	public void setConfigResourceFilename(String configResourceFilename) {
		log.info("**** Setting config.xml to: " + configResourceFilename);
		this.configResourceFilename = configResourceFilename;
	}
	
	@Override
	public List<MailItemInstance<Object>> getActiveMailItemInstances() {
		Active activeItems = this.items.getActive();
		List<MailItemInstance<Object>> items = new ArrayList<>();
		for (engine.dataobjects.xml.MailItem item : activeItems.getMailItem()) {
			MailItem<engine.dataobjects.xml.MailItem> mi = new MailItem<engine.dataobjects.xml.MailItem>();
			mi.setMaximumWidthImage(item.getMaximumWidthImage());
			mi.setId(item.getId());
			mi.setName(item.getName());
			mi.setParser(item.getParser());
			mi.setUrl(item.getUrl());
			mi.setXsltFile(item.getXsltFile());
			MailItemInstance<Object> instance = new MailItemInstance(mi);
			
			instance.setTo(item.getTo().getEmail());
			instance.setJobInterval(item.getJobInterval());
			items.add(instance);
		}
		return items;
	}
	
	@Override
	public List<MailItem<engine.dataobjects.xml.MailItem>> getAllMailItems() {
		Active activeItems = this.items.getActive();
		Inactive inActiveItems = this.items.getInactive();
		activeItems.getMailItem().addAll(inActiveItems.getMailItem());
		List<MailItem<engine.dataobjects.xml.MailItem>> items = new ArrayList<>();
		for (engine.dataobjects.xml.MailItem item : activeItems.getMailItem()) {
			MailItem<engine.dataobjects.xml.MailItem> mi = new MailItem<engine.dataobjects.xml.MailItem>();
			mi.setMaximumWidthImage(item.getMaximumWidthImage());
			mi.setId(item.getId());
			mi.setName(item.getName());
			mi.setParser(item.getParser());
			mi.setUrl(item.getUrl());
			mi.setXsltFile(item.getXsltFile());
			mi.setDataObject(item);
			items.add(mi);
		}
		return items;
	}

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
		
	}

	@Override
	public void setAutoSave(boolean isAutoSave) {
		throw new RuntimeException("not supported");
	}

	@Override
	public void addMailItem(MailItem<engine.dataobjects.xml.MailItem> item) throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public void updateMailItem(MailItem<engine.dataobjects.xml.MailItem> item) throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public void deleteMailItem(MailItem<engine.dataobjects.xml.MailItem> item) throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public MailItem<engine.dataobjects.xml.MailItem> getMailItem(String id) throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public void save() throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public void addMailItemInstance(MailItemInstance<Object> item)
			throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public void updateMailItemInstance(MailItemInstance<Object> item)
			throws Exception {
		throw new RuntimeException("not supported");
	}

	@Override
	public void deleteMailItemInstance(MailItemInstance<Object> item)
			throws Exception {
		throw new RuntimeException("not supported");
	}

}
